package com.ruoyi.web.controller.ydz;

import java.util.List;

import com.ruoyi.web.domain.Doctor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.Hospital;
import com.ruoyi.web.service.IHospitalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医院Controller
 * 
 * @author why
 * @date 2025-05-13
 */
@Controller
@RequestMapping({"/ydz/hospital","/api/hospital"})
@Api(tags = "医院")
public class HospitalController extends BaseController
{
    private String prefix = "ydz/hospital";

    @Autowired
    private IHospitalService hospitalService;

    @RequiresPermissions("ydz:hospital:view")
    @GetMapping()
    public String hospital()
    {
        return prefix + "/hospital";
    }

    /**
     * 查询医院列表
     */
    @RequiresPermissions("ydz:hospital:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询医院列表")
    public TableDataInfo list(Hospital hospital)
    {
        startPage();
        List<Hospital> list = hospitalService.selectHospitalList(hospital);
        return getDataTable(list);
    }

    @PostMapping("/getHospitalList")
    @ResponseBody
    public TableDataInfo getHospitalList(@RequestBody Hospital hospital){
        startPage();
        List<Hospital> list = hospitalService.selectHospitalList(hospital);
        return getDataTable(list);
    }

    /**
     * 导出医院列表
     */
    @RequiresPermissions("ydz:hospital:export")
    @Log(title = "医院", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation("导出医院列表")
    public AjaxResult export(Hospital hospital)
    {
        List<Hospital> list = hospitalService.selectHospitalList(hospital);
        ExcelUtil<Hospital> util = new ExcelUtil<Hospital>(Hospital.class);
        return util.exportExcel(list, "医院数据");
    }

    /**
     * 新增医院
     */
    @GetMapping("/add")
    @ApiOperation("新增医院")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医院
     */
    @ApiOperation("新增保存医院")
    @RequiresPermissions("ydz:hospital:add")
    @Log(title = "医院", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody Hospital hospital)
    {
        return toAjax(hospitalService.insertHospital(hospital));
    }

    /**
     * 修改医院
     */
    @ApiOperation("修改医院")
    @RequiresPermissions("ydz:hospital:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Hospital hospital = hospitalService.selectHospitalById(id);
        mmap.put("hospital", hospital);
        return prefix + "/edit";
    }

    /**
     * 修改保存医院
     */
    @ApiOperation("修改保存医院")
    @RequiresPermissions("ydz:hospital:edit")
    @Log(title = "医院", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Hospital hospital)
    {
        return toAjax(hospitalService.updateHospital(hospital));
    }

    /**
     * 删除医院
     */
    @ApiOperation("删除医院")
    @RequiresPermissions("ydz:hospital:remove")
    @Log(title = "医院", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(hospitalService.deleteHospitalByIds(ids));
    }

    /*
     * 获取医生详情
     * */
    @ApiOperation("获取医院详情")
    @GetMapping("/getHospitalById/{id}")
    @ResponseBody
    public AjaxResult getHospitalById(@PathVariable("id") Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            Hospital hospital = hospitalService.selectHospitalById(id);
            ajaxResult.put("data",hospital);
            ajaxResult.put("code",0);
            ajaxResult.put("msg","查询成功！");
        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取医院详情失败，请联系管理员！");
        }
        return ajaxResult;
    }
}
